package com.ansion.commonchat.contact.ui;

import android.util.Pair;

import com.ansion.commonchat.contact.bean.ContactBaseBean;
import com.ansion.commonchat.contact.bean.ContactDividerBean;
import com.ansion.commonchat.contact.bean.ContactHeaderBean;
import com.ansion.string.StringUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


public class ContactIndexHelper {

    public static Pair<List<ContactBaseBean>, List<String>> getIndex(List<ContactBaseBean> itemList, boolean needPositioning) {
        List<String> keys = new ArrayList<String>();
        List<ContactBaseBean> returnList = new
                LinkedList<>();
        for (int i = 0; i < itemList.size(); i++) {
            if (i == 0) {
                String indexDisplay = itemList.get(i).getIndexDisplay();
                returnList.add(new ContactDividerBean(indexDisplay));
                returnList.add(new ContactHeaderBean(indexDisplay, needPositioning));
                if (needPositioning) {
                    keys.add(itemList.get(i).getIndexDisplay());
                }
            } else {
                if (StringUtils.compareString(itemList.get(i - 1)
                        .getIndexDisplay(), itemList.get(i).getIndexDisplay()) != 0) {
                    String indexDisplay = itemList.get(i).getIndexDisplay();
                    returnList.add(new ContactDividerBean(indexDisplay));
                    returnList.add(new ContactHeaderBean(indexDisplay, needPositioning));
                    if (needPositioning) {
                        keys.add(itemList.get(i).getIndexDisplay());
                    }
                }
            }
            returnList.add(itemList.get(i));
        }

        return new Pair<>(returnList, keys);
    }
}
